﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Utils.MathUtilitys;

namespace Utils.Menu.Buttons
{
    public abstract class AbstractButton
    {
        bool inside;
        bool pressed;
        float hoover;
        float hooverTime = 0.1f;
        ButtonListener listener;

        public bool Inside { get { return inside; } }
        public float Hoover { get { return hoover; } set { hoover = value; } }
        public float HooverTime { get { return hooverTime; } set { hooverTime = value; } }

        public AbstractButton(ButtonListener listener)
        {
            this.listener = listener;
        }

        public virtual void UpdateMousePosition(Vector2 mousePosition)
        {
            inside = MouseInside(mousePosition);
        }

        public virtual void Update(float dTime)
        {
            if (inside)
            {
                if (hooverTime <= 0)
                {
                    hoover = 1;
                }
                else
                {
                    hoover = MathUtils.Cap(hoover + dTime / hooverTime, 0, 1);
                }
            }
            else
            {
                if (hooverTime <= 0)
                {
                    hoover = 0;
                }
                else
                {
                    hoover = MathUtils.Cap(hoover - dTime / hooverTime, 0, 1);
                }
            }

            if (!inside && pressed)
            {
                Release();
            }
        }

        public virtual bool Press()
        {
            if (!pressed)
            {
                pressed = true;
                if (listener != null)
                {
                    listener.ButtonPressed(this);
                }
                return true;
            }

            return false;
        }

        public virtual bool Release()
        {
            if (pressed)
            {
                pressed = false;
                if (listener != null)
                {
                    listener.ButtonReleased(this);
                }
                return true;
            }
            return false;
        }

        public abstract bool MouseInside(Vector2 mousePosition);
    }
}
